﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SAD_SiS.Models;
using SAD_SiS.ViewModels;

namespace SAD_SiS.Controllers
{
    public class CategoryController : Controller
    {
        //
        // GET: /Category/
        private int PageSize = 8;
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Views(string id, int page = 1)
        {
            using (SADEntities dbContext = new SADEntities())
            {
                PagingView returnView = new PagingView();
                var category = (from c in dbContext.Categories where c.LinkName.Equals(id) select c).SingleOrDefault();
                if (category == null)
                {
                    return View("Error");
                }
                returnView.SelectCate = category;
                if (category.isLeaf == true)
                {
                    var productList = (from p in dbContext.V_Product where p.CateID == category.CateID && p.Active == true orderby p.CreatedDate descending select p).ToList();
                    returnView.TotalItem = productList.Count;

                    returnView.ItemsPerPage = PageSize;
                    returnView.CurrentPage = page;
                    returnView.TotalPage = (int)Math.Ceiling((decimal)returnView.TotalItem / returnView.ItemsPerPage);
                    returnView.ProductList = productList.Skip((page - 1) * PageSize).Take(PageSize).ToList();
                }
                return View(returnView);
            }

        }
    }
}
